//
//  MainViewController.m
//  A One POS
//
//  Created by AonePOS on 24/09/13.
//  Copyright (c) 2013 AonePOS. All rights reserved.
//

#import "MainView.h"
#import "DataBaseManager.h"
#import "ServersyncClass.h"
#import "general data.h"
#define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v)  ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending)
@interface MainView ()

@end

@implementation MainView
{
     ServersyncClass *serverSyncClassObject;
    general_data *generalDataObject;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
   
    generalDataObject =[general_data new];
    [generalDataObject  GetuserDataVariablesMethod];
    serverSyncClassObject=[ServersyncClass new];
    
    
    //[NSThread detachNewThreadSelector:@selector(ServerSyncronizationMethod) toTarget:serverSyncClassObject withObject:nil];
    [super viewDidLoad];
      //appDelegate =  (AppDelegate *)[UIApplication sharedApplication].delegate;
    
  NSString *taxSelectQuery = [NSString stringWithFormat:@"SELECT name,tax_value FROM taxes"];
    NSMutableArray *taxesArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager]execute:taxSelectQuery resultsArray:taxesArray];
    if ([taxesArray count]==0){
        
        NSArray *taxnamesArray=[[NSArray alloc]initWithObjects:@"tax1",@"tax2",@"tax3",nil];
        NSArray *taxratesArray=[[NSArray alloc]initWithObjects:@"8.5",@"0",@"0",nil];
        
        for (int i=0; i<[taxnamesArray count]; i++){
            NSString *taxname=[taxnamesArray objectAtIndex:i];
            NSString *rate=[taxratesArray objectAtIndex:i];
            
            
            NSString *insertSQL =@"insert into taxes (unique_id,created_timestamp,modified_timestamp,name,tax_value) values(?,?,?,?,?)";
            NSMutableArray *taxArray=[[NSMutableArray alloc]initWithObjects:[generalDataObject  genRandStringLength:20],[generalDataObject  Datestring],[generalDataObject  Datestring],taxname,rate, nil];
            [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertSQL withArguments:taxArray];
            
            insertSQL=Nil;
            [taxArray removeAllObjects];
        }
    }
     [taxesArray removeAllObjects];
     NSString *printerSelectQuery = [NSString stringWithFormat: @"SELECT printname,print_id,ipaddress FROM printer"];
    NSMutableArray *printersArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager]execute:printerSelectQuery resultsArray:printersArray];
    if ([printersArray count]==0){
        NSArray *printertypesArray=[[NSArray alloc]initWithObjects:@"STAR",@"EPSON", nil];
        NSArray *printerNamesArray=[[NSArray alloc]initWithObjects:@"TSP1000",@"TM-T88V", nil];
        NSArray *printerIPsArray=[[NSArray alloc]initWithObjects:@"192.168.1.168",@"192.168.1.167", nil];
        NSArray *printerIDsArray=[[NSArray alloc]initWithObjects:@"printer1",@"printer2", nil];
        @autoreleasepool {
        for (int i=0; i<[printertypesArray count]; i++)
        {
           NSString *printername=[printerNamesArray objectAtIndex:i];
            NSString *printerType=[printertypesArray objectAtIndex:i];
            NSString *printerIP=[printerIPsArray objectAtIndex:i];
            NSString *printerID=[printerIDsArray objectAtIndex:i];
            NSString *insertSQL =@"insert into printer (headlines, language,unit,spacing,fontsize,align,Wsize,Hsize,bold,underline,Xposition,printname,print_id,ipaddress,created_timestamp,modified_timestamp,unique_id,printer_type,searchprinter) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
            
              NSMutableArray *printerArray=[[NSMutableArray alloc]initWithObjects:@"Aonepos",@"ANK",@"1",@"30",@"A",@"Left",@"1",@"1",@"0",@"0",@"1",printername,printerID,printerIP,[generalDataObject  Datestring],[generalDataObject  Datestring],[generalDataObject  genRandStringLength:20],printerType,@"TCP", nil];
          
            [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertSQL withArguments:printerArray];
            insertSQL=nil;
            [printersArray removeAllObjects];
        }
       }

    }
    [printersArray removeAllObjects];
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
   
    
    
    y=0;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")){
        y=y+20;
    }
    CGFloat width,height;
    width=self.view.frame.size.width;
    height=self.view.frame.size.height;
    // //NSLog(@" %f,%f",width,height);
    
    
    logobackview=[[UIImageView alloc]init];
    logobackview.frame=CGRectMake(0.0, y,width , 170);
    logobackview.userInteractionEnabled=YES;
    //NSString *thePath = [[NSBundle mainBundle] pathForResource:@"sampleImage" ofType:@"png"];
    //UIImage *newImage =  [[UIImage alloc] initWithContentsOfFile:thePath];
    //[logobackview setImage:[UIImage imageNamed:@"logo-login-bg.png"]];
    
    [logobackview setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logo-login-bg" ofType:@"png"]]];
    
    [self.view addSubview:logobackview];
    
    logoimageview=[[UIImageView alloc]init];
    logoimageview.frame=CGRectMake(50, 10,150, 150);
    //[logoimageview setImage:[UIImage imageNamed:@"aoneposlogo.png"]];
    [logoimageview setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"aoneposlogo" ofType:@"png"]]];
    [logobackview addSubview:logoimageview];
    
    helpiconButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [helpiconButton setFrame:CGRectMake(width-180,y+40,70,70)];
    [helpiconButton addTarget:self action:@selector(helpiconclicked) forControlEvents:UIControlEventTouchUpInside];
    
    //[helpiconButton setImage:[UIImage imageNamed:@"helpicon.png"] forState:UIControlStateNormal];
    [helpiconButton setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"helpicon" ofType:@"png"]] forState:UIControlStateNormal];
    
    [logobackview addSubview:helpiconButton];
    logouticonButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [logouticonButton setFrame:CGRectMake(width-100,y+40,70,70)];
    [logouticonButton addTarget:self action:@selector(logoutButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    
    //[logouticonButton setImage:[UIImage imageNamed:@"logoutbutton.png"] forState:UIControlStateNormal];
    [logouticonButton setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"logoutbutton" ofType:@"png"]] forState:UIControlStateNormal];
    [logobackview addSubview:logouticonButton];
    
    
    backgroundcolorview=[[UIView alloc]init];
    backgroundcolorview.frame=CGRectMake(0,(logoimageview.frame.size.height)+y+20,width,(height-(logoimageview.frame.size.height)));
    [backgroundcolorview  setBackgroundColor:[UIColor colorWithRed:0.0f green:74.0f/255.0f blue:132.0f/255.0f alpha:1.0f]];
    [self.view addSubview:backgroundcolorview];
    CGFloat buttonwidth=(width/3)-20;
    CGFloat buttonvertical=(height/4)-100;
    CGFloat buttonheight=150;
    @autoreleasepool {
    homeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [homeBtn setFrame:CGRectMake(30,y+buttonvertical,buttonwidth, buttonheight)];
    [homeBtn addTarget:self action:@selector(homebuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    homeBtn.tag=1;
    // [homeBtn setImage:[UIImage imageNamed:@"posnormal.png"] forState:UIControlStateNormal];
    [homeBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"posnormal" ofType:@"png"]] forState:UIControlStateNormal];
    
    [backgroundcolorview addSubview:homeBtn];
    
    inventoryBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    
    [inventoryBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical,buttonwidth, buttonheight)];
    [inventoryBtn addTarget:self action:@selector(inventorybuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    inventoryBtn.tag=3;
    //[inventoryBtn setImage:[UIImage imageNamed:@"inventorynormal.png"] forState:UIControlStateNormal];
    [inventoryBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inventorynormal" ofType:@"png"]] forState:UIControlStateNormal];
    [backgroundcolorview addSubview:inventoryBtn];
    
  
    helpBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [helpBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical,buttonwidth, buttonheight)];
    [helpBtn addTarget:self action:@selector(helpbuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    helpBtn.tag=9;
    //[helpBtn setImage:[UIImage imageNamed:@"storespsd.png"] forState:UIControlStateNormal];
    [helpBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"storespsd" ofType:@"png"]] forState:UIControlStateNormal];
    [backgroundcolorview addSubview:helpBtn];
    
    
    customerBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [customerBtn setFrame:CGRectMake(30,y+buttonvertical*2,buttonwidth, buttonheight)];
    [customerBtn addTarget:self action:@selector(customerbuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    customerBtn.tag=4;
    //[customerBtn setImage:[UIImage imageNamed:@"customeractive.png"] forState:UIControlStateNormal];
    [customerBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"customeractive" ofType:@"png"]] forState:UIControlStateNormal];
    [backgroundcolorview addSubview:customerBtn];
    
    employeeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [employeeBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical*2,buttonwidth, buttonheight)];
    [employeeBtn addTarget:self action:@selector(employeebuttonClikced) forControlEvents:UIControlEventTouchUpInside];
    employeeBtn.tag=5;
    [employeeBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"employeenormal" ofType:@"png"]] forState:UIControlStateNormal];
    [backgroundcolorview addSubview:employeeBtn];
    
    settingsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [settingsBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical*2,buttonwidth, buttonheight)];
    [settingsBtn addTarget:self action:@selector(settingsButtonClicked) forControlEvents:UIControlEventTouchUpInside];
    settingsBtn.tag=6;
    //[settingsBtn setImage:[UIImage imageNamed:@"settingsnormal.png"] forState:UIControlStateNormal];
    [settingsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"settingsnormal" ofType:@"png"]] forState:UIControlStateNormal];
    
    [backgroundcolorview addSubview:settingsBtn];
    
    reportsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [reportsBtn setFrame:CGRectMake(30,y+buttonvertical*3,buttonwidth, buttonheight)];
    [reportsBtn addTarget:self action:@selector(reportsbuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    reportsBtn.tag=7;
    //[reportsBtn setImage:[UIImage imageNamed:@"reportsnormal.png"] forState:UIControlStateNormal];
    [reportsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"reportsnormal" ofType:@"png"]] forState:UIControlStateNormal];
    
    [backgroundcolorview addSubview:reportsBtn];
    
    ContactsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [ContactsBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical*3,buttonwidth, buttonheight)];
    [ContactsBtn addTarget:self action:@selector(contactsbuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    ContactsBtn.tag=8;
    //[ContactsBtn setImage:[UIImage imageNamed:@"contactsnormal.png"] forState:UIControlStateNormal];
    [ContactsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contactsnormal" ofType:@"png"]] forState:UIControlStateNormal];
    
    [backgroundcolorview addSubview:ContactsBtn];
    sellBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [sellBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical*3,buttonwidth, buttonheight)];
    
    [sellBtn addTarget:self action:@selector(sellbuttonClicked) forControlEvents:UIControlEventTouchUpInside];
    sellBtn.tag=2;
    //[sellBtn setImage:[UIImage imageNamed:@"profilenormal.png"] forState:UIControlStateNormal];
    
    [sellBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"profilenormal" ofType:@"png"]] forState:UIControlStateNormal];
    
    [backgroundcolorview addSubview:sellBtn];
    }
    
    y=0;
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0"))
    {
        y=y+20;
        
        
    }
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    
    [self updateUI];
    
   /* NSString *PselectQuery = [NSString stringWithFormat: @"SELECT inventory_item_no,inventary_item_name FROM inventorytable"];
    NSMutableArray *productArray=[[NSMutableArray alloc]init];
    [[DataBaseManager dataBaseManager]execute:PselectQuery resultsArray:productArray];
       if ([productArray count]==0 && [[generalDataObject  OriginalbaseURLstr] length]==0){
       
        //[NSThread detachNewThreadSelector:@selector(loadCSVData) toTarget:self withObject:nil];
    }*/

    helpiconButton.hidden=YES;
    // Do any additional setup after loading the view from its nib.
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    isLandScope = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
    [self updateUI];
}
-(void)loadCSVData
{
    NSStringEncoding encoding;
    NSString *path1=[[NSString alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"sampledata" ofType:@"csv"]  usedEncoding:&encoding error:nil];
    //You will get the array of lines
    NSArray *messArr=[path1 componentsSeparatedByString:@"\n"];
    
    //// //NSLog(@"Array total data %@",messArr);
    
    //Now start to process each single line.
    if(messArr)
    {
        for(int i=0;i<=[messArr count]-1;i++)
        {
            
            //NSMutableDictionary *d=[[NSMutableDictionary alloc] init];
            NSString *StrValue=[NSString stringWithFormat:@"%@",[messArr objectAtIndex:i]];
            
            StrValue=[StrValue stringByReplacingOccurrencesOfString:@"\"" withString:@""];
            StrValue=[StrValue stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
            NSArray *arr=[StrValue componentsSeparatedByString:@","];
            
            /*Add value for each column into dictionary*/
            
            //// //NSLog(@"%@",arr);
            
            if ([arr count]>9)
            {
                NSString *itemname=[NSString stringWithFormat:@"%@",[arr objectAtIndex:0]];
                NSString *itemno=[NSString stringWithFormat:@"%@",[arr objectAtIndex:1]];
                NSString *department=[NSString stringWithFormat:@"%@",[arr objectAtIndex:2]];
                
                NSString *cost=[NSString stringWithFormat:@"%@",[arr objectAtIndex:3]];
                NSString *stock=[NSString stringWithFormat:@"%@",[arr objectAtIndex:4]];
                //NSString *ItemNO=[NSString stringWithFormat:@"%@",[arr objectAtIndex:5]];
                NSString *price=[NSString stringWithFormat:@"%@",[arr objectAtIndex:6]];
                NSString *vendorno=[NSString stringWithFormat:@"%@",[arr objectAtIndex:7]];
                // NSString *vendorname=[NSString stringWithFormat:@"%@",[arr objectAtIndex:8]];
                NSString *catid=[NSString stringWithFormat:@"%@",[arr objectAtIndex:9]];
                ///NSString *catid=[NSString stringWithFormat:@"%@",[arr objectAtIndex:10]];
                
                NSString *insertSQL =@"insert into inventorytable (inventary_department,inventory_item_no,inventary_item_name,inventary_second_description,inventary_avg_cost,inventary_price_tax,inventary_price_change,inventary_in_stock,inventary_quantity,inventary_taxone,inventary_vendor,unique_id,created_timestamp,modified_timestamp,inventory_total_tax,inventary_notes,check_value,category_id) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                NSMutableArray *invArray=[[NSMutableArray alloc]initWithObjects:department,itemno,itemname,@"description",cost,price,price,stock,@"1",@"",vendorno,[generalDataObject  genRandStringLength:20],[generalDataObject  Datestring],[generalDataObject  Datestring],@"8.25",@"",@"true",catid, nil];
                [[DataBaseManager dataBaseManager]executeRecordsWithQuery:insertSQL withArguments:invArray];
                
                NSString *optiopnalinsertSQL =@"insert into optional_info_table (bonus_points,barcodes,location,unit_type,unit_size,commission_optional_info,inventary_modifier_item,inventary_count_this_item,inventary_prompt_quantity,inventary_prompt_price,inventary_print_on_receipt,inventary_foodstampable,inventory_item_no,unique_id,created_timestamp,modified_timestamp) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
                
                NSMutableArray *optArray=[[NSMutableArray alloc]initWithObjects:@"",@"",@"",@"",@"",@"",@"no",@"no",@"no",@"no",@"no",@"no",itemno,[generalDataObject  genRandStringLength:20],[generalDataObject  Datestring],[generalDataObject  Datestring], nil];
                
                [[DataBaseManager dataBaseManager]executeRecordsWithQuery:optiopnalinsertSQL withArguments:optArray];
                
                NSMutableArray *DepartmentNamesArray=[[NSMutableArray alloc]init];
                NSString *departmentSelectQuery = [NSString stringWithFormat: @"SELECT department_id,department_description FROM department_details"];
                NSMutableArray *deptArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:departmentSelectQuery resultsArray:deptArray];
                for (NSDictionary *myDict in deptArray )
                {
                    
                    NSString *deptname=[myDict objectForKey:@"department_id"];
                    
                    [DepartmentNamesArray addObject:deptname];
                    
                }
            NSUInteger objIdx = [DepartmentNamesArray containsObject:department];
                
                // //NSLog(@"%lu",(unsigned long)objIdx);
                if(objIdx == 0) {
                    // // //NSLog(@"condition true");
                    
                    NSString *DeptinsertSQL =@"insert into department_details(department_id,department_description,category_id,unique_id,created_timestamp,modified_timestamp,department_foodstampable,department_totaltax,check_value) values(?,?,?,?,?,?,?,?,?)";
                    NSMutableArray *DeptinsertArray=[[NSMutableArray alloc]initWithObjects:department,@"Description",catid,@"",[generalDataObject  Datestring],[generalDataObject  Datestring],@"",@"",@"true", nil];
                    
                    [[DataBaseManager dataBaseManager]executeRecordsWithQuery:DeptinsertSQL withArguments:DeptinsertArray];
                    
                }
                
               NSMutableArray *categorynamesArray=[[NSMutableArray alloc]init];
                  NSString *catSelectQuery = [NSString stringWithFormat: @"SELECT category_id,category_description FROM category_details"];
                NSMutableArray *catArray=[[NSMutableArray alloc]init];
                [[DataBaseManager dataBaseManager]execute:catSelectQuery resultsArray:catArray];
               
              for (NSDictionary *myDict in catArray)
                {
                    
                    NSString *catname=[myDict objectForKey:@"category_id"];
                    
                    [categorynamesArray addObject:catname];
                    
                }
                NSUInteger objIdx2 = [categorynamesArray containsObject:catid];
                // //NSLog(@"%lu",(unsigned long)objIdx);
                if(objIdx2 == 0) {
                    //// //NSLog(@"condition true");
                    NSString *CatinsertSQL =@"insert into category_details(category_id,category_description,unique_id,created_timestamp,modified_timestamp) values(?,?,?,?,?)";
                    NSMutableArray *catinsertArray=[[NSMutableArray alloc]initWithObjects:catid,@"Description",[generalDataObject  genRandStringLength:20],[generalDataObject  Datestring],[generalDataObject  Datestring], nil];
                    [[DataBaseManager dataBaseManager]executeRecordsWithQuery:CatinsertSQL withArguments:catinsertArray];
                    
                }
                
                
               /* NSMutableArray *VendornumbersArray=[[NSMutableArray alloc]init];
                
                  NSString *querySQL = [NSString stringWithFormat: @"SELECT vendor_no FROM vendor"];
                for (NSDictionary *myDict in [dbObject getdataMethod:querySQL])
                {
                    
                    NSString *catname=[myDict objectForKey:@"vendor_no"];
                    
                    [VendornumbersArray addObject:catname];
                    
                }
                
                NSUInteger objIdx3 = [VendornumbersArray containsObject:vendorno];
                // //NSLog(@"%lu",(unsigned long)objIdx);
                if(objIdx3 == 0)
                {
                    
                    //// //NSLog(@"condition true");
                    
                    //NSString *insertSQL = [NSString stringWithFormat: @"insert into vendor (vendor_no,vendor_terms,vendor_min_order,vendor_commission_percent,vendor_company_name,vendor_flat_rent_rate,vendor_tax_id,vendor_billable_department,vendor_social_security_no,vendor_po_delivery_method,vendor_street_address,vendor_extended_address,vendor_city,vendor_state,vendor_zip_code,vendor_country,vendor_first_name,vendor_last_name,vendor_telephone_number,vendor_fax_number,vendor_email,vendor_website,unique_id,created_timestamp,modified_timestamp) values('%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@','%@')",vendorno,@"",@"",@"",vendorname,@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",@"",[generalDataObject  genRandStringLength:20],[generalDataObject  Datestring],[generalDataObject  Datestring]];
                    //[[OriginalDB getSharedInstance]SaveEmployeeData:insertSQL];
                    
                    
                }*/
            }
            
        }
    }
   
    //[generalDataObject  setSampleDataloadValue:@"yes"];
    
}

- (BOOL)shouldAutorotate
{
    // //NSLog(@"called");
    
    
    return YES;
    
    
}
- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration

{
    if (toInterfaceOrientation==UIInterfaceOrientationLandscapeRight||toInterfaceOrientation==UIInterfaceOrientationLandscapeLeft)
    {
        isLandScope=YES;
        
    }else
    {
        isLandScope=NO;
    }
    [self updateUI];
}
-(void)updateUI
{
    if(isLandScope)
    {
        //CGRect frame=self.view.frame;
        // //NSLog(@"self frame %@ ",NSStringFromCGRect(frame));
        CGFloat width,height;
        width=1024;
        height=748;
        // //NSLog(@" %f,%f",width,height);
        
        
        logobackview.frame=CGRectMake(0.0, y,width , 170);
        logoimageview.frame=CGRectMake(50, 10.0,150, 150);
        // logoimageview.frame=CGRectMake(50, y+20,150, 150);
        [helpiconButton setFrame:CGRectMake(width-180,y+40,70,70)];
        [logouticonButton setFrame:CGRectMake(width-100,y+40,70,70)];
        backgroundcolorview.frame=CGRectMake(0,(logoimageview.frame.size.height)+y+20,width,(height-(logoimageview.frame.size.height)));
        CGFloat buttonwidth=(width/3)-20;
        CGFloat buttonheight=150;
        CGFloat buttonvertical=(backgroundcolorview.frame.size.width/4)-150;
        // //NSLog(@"%f",backgroundcolorview.frame.size.height);
        [homeBtn setFrame:CGRectMake(30,y+buttonvertical,buttonwidth, buttonheight)];
        
        
        [sellBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical,buttonwidth, buttonheight)];
        
        [inventoryBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical,buttonwidth, buttonheight)];
        
        [customerBtn setFrame:CGRectMake(30,((y+buttonvertical)*2),buttonwidth, buttonheight)];
        
        
        [employeeBtn setFrame:CGRectMake((buttonwidth+30),((y+buttonvertical)*2),buttonwidth, buttonheight)];
        
        [settingsBtn setFrame:CGRectMake(((buttonwidth*2)+30),((y+buttonvertical)*2),buttonwidth, buttonheight)];
        
        [reportsBtn setFrame:CGRectMake(30,((y+buttonvertical)*3),buttonwidth, buttonheight)];
        [ContactsBtn setFrame:CGRectMake((buttonwidth+30),((y+buttonvertical)*3),buttonwidth, buttonheight)];
        
        [helpBtn setFrame:CGRectMake(((buttonwidth*2)+30),((y+buttonvertical)*3),buttonwidth, buttonheight)];
    }
    
    else
    {
        
        CGFloat width,height;
        height=1004;
        width=768;
        // //NSLog(@" %f,%f",width,height);
        
        CGFloat buttonwidth=(width/3)-20;
        CGFloat buttonvertical=(height/4)-100;
        logobackview.frame=CGRectMake(0.0, y,width , 170);
        logoimageview.frame=CGRectMake(50, 10,150, 150);
        //logoimageview.frame=CGRectMake(50, y+20,150, 150);
        [helpiconButton setFrame:CGRectMake(width-180,y+40,70,70)];
        [logouticonButton setFrame:CGRectMake(width-100,y+40,70,70)];
        backgroundcolorview.frame=CGRectMake(0,(logoimageview.frame.size.height)+y+20,width,(height-(logoimageview.frame.size.height)));
        CGFloat buttonheight=150;
        
        [homeBtn setFrame:CGRectMake(30,y+buttonvertical,buttonwidth, buttonheight)];
        
        
        [sellBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical,buttonwidth, buttonheight)];
        
        [inventoryBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical,buttonwidth, buttonheight)];
        
        [customerBtn setFrame:CGRectMake(30,y+buttonvertical*2,buttonwidth, buttonheight)];
        
        
        [employeeBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical*2,buttonwidth, buttonheight)];
        
        [settingsBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical*2,buttonwidth, buttonheight)];
        
        [reportsBtn setFrame:CGRectMake(30,y+buttonvertical*3,buttonwidth, buttonheight)];
        [ContactsBtn setFrame:CGRectMake((buttonwidth+30),y+buttonvertical*3,buttonwidth, buttonheight)];
        
        [helpBtn setFrame:CGRectMake(((buttonwidth*2)+30),y+buttonvertical*3,buttonwidth, buttonheight)];
    }
    
}
-(void)homebuttonClicked
{
    [homeBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"pospressed" ofType:@"png"]] forState:UIControlStateNormal];
     Home *homeObj=[[Home alloc]initWithNibName:@"Home" bundle:nil];
    [self.navigationController pushViewController:homeObj animated:NO];
    [self releaseViews];
    
}
-(void)sellbuttonClicked
{
    if ([[generalDataObject  userType] isEqualToString:@"admin"])
    {
        //[sellBtn setImage:[UIImage imageNamed:@"profileactive.png"] forState:UIControlStateNormal];
         [sellBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"profileactive" ofType:@"png"]] forState:UIControlStateNormal];
        
        Profile *profileView=[[Profile alloc]initWithNibName:@"Profile" bundle:nil];
        [self.navigationController pushViewController:profileView animated:NO];
        [self releaseViews];
    }else{
        UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
        
        [authalert show];
        
    }
    // [self releaseViews];
}
-(void)inventorybuttonClicked
{
    if ([[generalDataObject  userType] isEqualToString:@"employee"])
    {
        if ([[generalDataObject  inventory_permission] isEqualToString:@"yes"])
        {
           // [inventoryBtn setImage:[UIImage imageNamed:@"inventoryactive.png"] forState:UIControlStateNormal];
            
             [inventoryBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inventoryactive" ofType:@"png"]] forState:UIControlStateNormal];
            Inventory *inventoryView=[[Inventory alloc]initWithNibName:@"Inventory" bundle:nil];
            [self.navigationController pushViewController:inventoryView animated:NO];
              [self releaseViews];
          
        }else{
            
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
        }
    }else{
        [inventoryBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"inventoryactive" ofType:@"png"]] forState:UIControlStateNormal];
        Inventory *inventoryView=[[Inventory alloc]initWithNibName:@"Inventory" bundle:nil];
        [self.navigationController pushViewController:inventoryView animated:NO];
        [self releaseViews];
    }
   
}

-(void)customerbuttonClicked
{
    if ([[generalDataObject  userType] isEqualToString:@"employee"]){
        if ([[generalDataObject  customer_permission] isEqualToString:@"yes"])
        {
            //[customerBtn setImage:[UIImage imageNamed:@"customernormal.png"] forState:UIControlStateNormal];
              [customerBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"customernormal" ofType:@"png"]] forState:UIControlStateNormal];
            Customers *customersView=[[Customers alloc]initWithNibName:@"Customers" bundle:nil];
            [self.navigationController pushViewController:customersView animated:NO];
            [self releaseViews];
        }else{
            
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
        }
    }else{
         [customerBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"customernormal" ofType:@"png"]] forState:UIControlStateNormal];
        Customers *customersView=[[Customers alloc]initWithNibName:@"Customers" bundle:nil];
        [self.navigationController pushViewController:customersView animated:NO];
        [self releaseViews];
    }
    
}
-(void)employeebuttonClikced
{
    //[employeeBtn setImage:[UIImage imageNamed:@"employeepressed.png"] forState:UIControlStateNormal];
    if ([[generalDataObject  userType] isEqualToString:@"admin"]){
      [employeeBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"employeepressed" ofType:@"png"]] forState:UIControlStateNormal];
    
        Employees *employeesView=[[Employees alloc]initWithNibName:@"Employees" bundle:nil];
        [self.navigationController pushViewController:employeesView animated:NO];
        [self releaseViews];

    }else{
        UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
        
        [authalert show];
    }
    // [self releaseViews];
    
}
-(void)settingsButtonClicked
{
    if ([[generalDataObject  userType] isEqualToString:@"employee"]){
        if ([[generalDataObject  settings_permission] isEqualToString:@"yes"]){
            //[settingsBtn setImage:[UIImage imageNamed:@"settingspressed.png"] forState:UIControlStateNormal];
            [settingsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"settingspressed" ofType:@"png"]] forState:UIControlStateNormal];
            Settings *settingsView=[[Settings alloc]initWithNibName:@"Settings" bundle:nil];
            [self.navigationController pushViewController:settingsView animated:NO];
            [self releaseViews];

        }else{
            
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
        }
    }else{
        
        [settingsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"settingspressed" ofType:@"png"]] forState:UIControlStateNormal];
        
        Settings *settingsView=[[Settings alloc]initWithNibName:@"Settings" bundle:nil];
        [self.navigationController pushViewController:settingsView animated:NO];
        [self releaseViews];

    }
    // [self releaseViews];
    
}
-(void)reportsbuttonClicked
{
    
    if ([[generalDataObject  userType] isEqualToString:@"employee"])
    {
        if ([[generalDataObject  settings_permission] isEqualToString:@"yes"])
        {
            //[reportsBtn setImage:[UIImage imageNamed:@"reportsactive.png"] forState:UIControlStateNormal];
            [reportsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"reportsactive" ofType:@"png"]] forState:UIControlStateNormal];
            Reports *reportsView=[[Reports alloc]initWithNibName:@"Reports" bundle:nil];
            [self.navigationController pushViewController:reportsView animated:NO];
            [self releaseViews];
        }else{
            
            UIAlertView *authalert=[[UIAlertView alloc]initWithTitle:@"Authentication:" message:@"User not authenticated" delegate:self cancelButtonTitle:@"ok" otherButtonTitles:Nil, nil];
            
            [authalert show];
        }
    }else{
         [reportsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"reportsactive" ofType:@"png"]] forState:UIControlStateNormal];
        
        Reports *reportsView=[[Reports alloc]initWithNibName:@"Reports" bundle:nil];
        [self.navigationController pushViewController:reportsView animated:NO];
         [self releaseViews];
    }
   
    
}

-(void)contactsbuttonClicked
{
    
    //[ContactsBtn setImage:[UIImage imageNamed:@"contactsactive.png"] forState:UIControlStateNormal];
      [ContactsBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"contactsactive" ofType:@"png"]] forState:UIControlStateNormal];
     Contacts *contactsView=[[Contacts alloc]initWithNibName:@"Contacts" bundle:nil];
    [self.navigationController pushViewController:contactsView animated:NO];
    [self releaseViews];
    
}
-(void)helpbuttonClicked
{
    
    //[helpBtn setImage:[UIImage imageNamed:@"storeactive.png"] forState:UIControlStateNormal];
    [helpBtn setImage:[[UIImage alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"storeactive" ofType:@"png"]] forState:UIControlStateNormal];
    
    Stores *storesView=[[Stores alloc]initWithNibName:@"Stores" bundle:nil];
    [self.navigationController pushViewController:storesView animated:NO];
     [self releaseViews];
    
}
-(void)helpiconclicked
{
    //[self dismissViewControllerAnimated:YES completion:nil];
    
}


- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    
    
    
    return YES;
    
}


- (NSUInteger)supportedInterfaceOrientations // iOS 6 autorotation fix
{
    
    return UIInterfaceOrientationMaskAll;
}
-(void)releaseViews
{
    
    NSArray *mainArray = self.view.subviews;
    for (int i = 0; i<[mainArray count]; i++) {
        UIView *vi =[mainArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
    
    NSArray *backArray = backgroundcolorview.subviews;
    for (int i = 0; i<[backArray count]; i++) {
        UIView *vi =[backArray objectAtIndex:i];
        [vi removeFromSuperview];
        vi = nil;
    }
   [backgroundcolorview removeFromSuperview];
    //[logoimageview removeFromSuperview],[logobackview removeFromSuperview];
     logoimageview=nil,logobackview=nil;
     backgroundcolorview=nil;
  //  [homeBtn removeFromSuperview], [inventoryBtn removeFromSuperview], [customerBtn removeFromSuperview], [employeeBtn removeFromSuperview], [settingsBtn removeFromSuperview], [reportsBtn removeFromSuperview], [ContactsBtn removeFromSuperview], [helpBtn removeFromSuperview];
    homeBtn=nil,sellBtn=nil,inventoryBtn=nil,customerBtn=nil,employeeBtn=nil,settingsBtn=nil,reportsBtn=nil,ContactsBtn=nil,helpBtn=nil;
    //[helpiconButton removeFromSuperview],[logouticonButton removeFromSuperview];
    helpiconButton=nil,logouticonButton=nil;
    [QuerytypesArray removeAllObjects],[TablenamesArray removeAllObjects],[FieldsArray removeAllObjects];
     QuerytypesArray=nil,TablenamesArray=nil,FieldsArray=nil;
    
     JsonSendDataDict=nil;
    
    
    [self.view removeFromSuperview];
    self.view=nil;
}

-(void)logoutButtonClicked
{
    // //NSLog(@"called ");
   
    [serverSyncClassObject LogoutMethod];
    
    NSString *updateSQL =@"UPDATE miscellaneous SET userType=?,sessionId=?,userID=?";
    NSMutableArray *misUpdatearray=[[NSMutableArray alloc]initWithObjects:@"",@"",@"", nil];
    [[DataBaseManager dataBaseManager] executeRecordsWithQuery:updateSQL withArguments:misUpdatearray];
    
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    // [self dismissViewControllerAnimated:NO completion:Nil];
     [self releaseViews];
    [generalDataObject  releasevariable];
    
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
